package com.jeeplus.test.jlnu.employmentInfo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AllInfo;
import com.jeeplus.test.jlnu.admissionInfo.mapper.AdmissionInfoMapper;
import com.jeeplus.test.jlnu.archivalInfo.domain.entity.ArchParmerter;
import com.jeeplus.test.jlnu.employmentInfo.domain.entity.EmploymentInfo;
import com.jeeplus.test.jlnu.employmentInfo.domain.vo.EmploymentInfoReqVo;
import com.jeeplus.test.jlnu.employmentInfo.domain.vo.EmploymentInfoRespVo;
import com.jeeplus.test.jlnu.employmentInfo.mapper.EmploymentInfoMapper;
import com.jeeplus.test.jlnu.employmentInfo.service.EmploymentInfoService;
import com.jeeplus.test.jlnu.fieldConfig.domain.FieldConfigEntity;
import com.jeeplus.test.jlnu.fieldConfig.mapper.EmploymentFieldConfigMapper;
import com.jeeplus.test.tools.resp.Convert2Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmploymentInfoServiceImpl extends ServiceImpl<EmploymentInfoMapper, EmploymentInfo>  implements EmploymentInfoService {

    @Autowired
    private EmploymentInfoMapper employmentInfoMapper;

    @Autowired
    private EmploymentFieldConfigMapper employmentFieldConfigMapper;


    @Override
    public Page<EmploymentInfoRespVo> getEmploymentInfoPage(ArchParmerter employmentInfoReqVo, Page<EmploymentInfoRespVo> page) {
        // 1. 获取分页结果和字段配置信息
        Page<EmploymentInfoRespVo> employmentInfoPage = employmentInfoMapper.getEmploymentInfoPage(employmentInfoReqVo, page);
//        List<FieldConfigEntity> allFieldConfigs = employmentFieldConfigMapper.getAllFieldConfigs();
//        List<AllInfo> records = employmentInfoPage.getRecords();
//
//        List<EmploymentInfoRespVo> modifiedRecords = new ArrayList<>();
//
//        // 2. 遍历每条记录
//        for (EmploymentInfoRespVo record : records) {
//            Map<String, Object> recordMap = new HashMap<>();
//
//            // 3. 将 ArchivalInfoRespVo 中的字段放入 map
//            Field[] fields = AllInfo.class.getDeclaredFields();
//            for (Field field : fields) {
//                field.setAccessible(true);
//                try {
//                    // 获取字段值并添加到 map 中
//                    Object value = field.get(record);
//                    recordMap.put(field.getName(), value);
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            // 4. 根据配置处理自定义字段
//            for (FieldConfigEntity config : allFieldConfigs) {
//                String originalField = config.getFieldKey();  // 原始字段名
//                String customField = config.getFieldName();   // 自定义字段名
//
//                try {
//                    // 获取 AdmissionInfoRespVo 中的原始字段
//                    Field originalFieldObj = AllInfo.class.getDeclaredField(originalField);
//                    originalFieldObj.setAccessible(true);
//
//                    // 获取字段值
//                    Object value = originalFieldObj.get(record);
//                    String prefixedCustomField = "custom_" + customField;
//                    // 将自定义字段名和值存入 map，覆盖默认字段名
//                    recordMap.put(prefixedCustomField, value);
//
//                    // 如果需要删除原始字段，可以选择性移除
//                    recordMap.remove(originalField); // 删除原始字段，保留自定义字段
//                } catch (NoSuchFieldException | IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            //这是我新放里的，将自定义字段放入到AllInfo中
//            AllInfo modifiedRecord = new AllInfo();
//            for (Map.Entry<String, Object> entry : recordMap.entrySet()) {
//                try {
//                    Field field = AllInfo.class.getDeclaredField(entry.getKey());
//                    field.setAccessible(true);
//                    field.set(modifiedRecord, entry.getValue()); // 设置新的 AllInfo 实例的字段值
//                } catch (NoSuchFieldException | IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            // 5. 添加处理后的记录
//            modifiedRecords.add(modifiedRecord);
//            //modifiedRecords.add(recordMap);
//        }

        // 6. 将处理后的记录封装到新的分页对象中返回
//        Page<EmploymentInfoRespVo> modifiedPage = new Page<>(employmentInfoPage.getCurrent(), employmentInfoPage.getSize(), employmentInfoPage.getTotal());
//        modifiedPage.setRecords(modifiedRecords);

        return employmentInfoPage;
    }

    @Override
    public Page<EmploymentInfoRespVo> selectempLoyList(EmploymentInfoReqVo employmentInfoReqVo, long current, long size) {
        EmploymentInfo employmentInfo = Convert2Bean.convertBean(employmentInfoReqVo, EmploymentInfo.class);
        LambdaQueryWrapper<EmploymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(employmentInfo.getId())) {
            queryWrapper.eq(EmploymentInfo::getId, employmentInfo.getId());
        }

        queryWrapper.orderByDesc(EmploymentInfo::getDateTime);
        IPage<EmploymentInfo> selectResult = employmentInfoMapper.selectPage(new Page<>(current, size, true), queryWrapper);
        List<EmploymentInfoRespVo> resultList = selectResult.getRecords().stream()
                .map(bean ->{
                    EmploymentInfoRespVo employmentInfoRespVo = Convert2Bean.convertBean(bean, EmploymentInfoRespVo.class);
                    AdmissionInfo admissionInfo = employmentInfoMapper.selectAdmissionById(employmentInfoRespVo.getStudentId());
                    employmentInfoRespVo.setArchivalNum(admissionInfo.getArchivalNum());
                    employmentInfoRespVo.setCaseTitle(admissionInfo.getCaseTitle());
                    employmentInfoRespVo.setFileTitle(admissionInfo.getFileTitle());
                    employmentInfoRespVo.setSaveLocation(admissionInfo.getSaveLocation());
                    employmentInfoRespVo.setPageLocation(admissionInfo.getPageLocation());
                    employmentInfoRespVo.setPageNum(admissionInfo.getPageNum());
                    employmentInfoRespVo.setExamineeNum(admissionInfo.getExamineeNum());
                    employmentInfoRespVo.setAdmissionNum(admissionInfo.getAdmissionNum());
                    employmentInfoRespVo.setName(admissionInfo.getName());
                    employmentInfoRespVo.setGender(admissionInfo.getGender());
                    employmentInfoRespVo.setBirthday(admissionInfo.getBirthday());
                    employmentInfoRespVo.setPoliticalOutlook(admissionInfo.getPoliticalOutlook());
                    employmentInfoRespVo.setNation(admissionInfo.getNation());
                    employmentInfoRespVo.setIdCard(admissionInfo.getIdCard());
                    employmentInfoRespVo.setPlanNature(admissionInfo.getPlanNature());
                    employmentInfoRespVo.setAdmissionSchools(admissionInfo.getAdmissionSchools());
                    employmentInfoRespVo.setAdmissionAcademy(admissionInfo.getAdmissionAcademy());
                    employmentInfoRespVo.setAdmissionMajor(admissionInfo.getAdmissionMajor());
                    employmentInfoRespVo.setAdmissionTime(admissionInfo.getAdmissionTime());
                    employmentInfoRespVo.setGradation(admissionInfo.getGradation());
                    employmentInfoRespVo.setEducationalSystem(admissionInfo.getEducationalSystem());
                    employmentInfoRespVo.setLearningForms(admissionInfo.getLearningForms());
                    employmentInfoRespVo.setExamineeSort(admissionInfo.getExamineeSort());
                    employmentInfoRespVo.setBatch(admissionInfo.getBatch());
                    employmentInfoRespVo.setGraduationSchool(admissionInfo.getGraduationSchool());
                    employmentInfoRespVo.setSourceArea(admissionInfo.getSourceArea());
                    employmentInfoRespVo.setAreaName(admissionInfo.getAreaName());
                    employmentInfoRespVo.setHomeAddress(admissionInfo.getHomeAddress());
                    employmentInfoRespVo.setPostalCode(admissionInfo.getPostalCode());
                    employmentInfoRespVo.setPhoneNum(admissionInfo.getPhoneNum());
                    employmentInfoRespVo.setInfoSource(admissionInfo.getInfoSource());
                    employmentInfoRespVo.setRemarks(admissionInfo.getRemarks());
                    employmentInfoRespVo.setExt1(admissionInfo.getExt1());
                    employmentInfoRespVo.setExt2(admissionInfo.getExt1());
                    employmentInfoRespVo.setExt3(admissionInfo.getExt2());
                    employmentInfoRespVo.setExt4(admissionInfo.getExt3());
                    employmentInfoRespVo.setExt5(admissionInfo.getExt4());
                    employmentInfoRespVo.setExt6(admissionInfo.getExt5());
                    employmentInfoRespVo.setExt7(admissionInfo.getExt6());
                    employmentInfoRespVo.setExt8(admissionInfo.getExt7());
                    employmentInfoRespVo.setExt9(admissionInfo.getExt8());
                    employmentInfoRespVo.setExt10(admissionInfo.getExt10());
                    employmentInfoRespVo.setExt11(admissionInfo.getExt11());
                    employmentInfoRespVo.setExt12(admissionInfo.getExt12());
                    employmentInfoRespVo.setExt13(admissionInfo.getExt13());
                    employmentInfoRespVo.setExt14(admissionInfo.getExt14());
                    employmentInfoRespVo.setExt15(admissionInfo.getExt15());
                    employmentInfoRespVo.setExt16(admissionInfo.getExt16());
                    employmentInfoRespVo.setExt17(admissionInfo.getExt17());
                    employmentInfoRespVo.setExt18(admissionInfo.getExt18());
                    employmentInfoRespVo.setExt19(admissionInfo.getExt19());
                    employmentInfoRespVo.setExt20(admissionInfo.getExt20());
                    return employmentInfoRespVo;
                } )
                .collect(Collectors.toList());
        Page<EmploymentInfoRespVo> page = new Page<>(current, size, true);
        page.setCurrent(selectResult.getCurrent());
        page.setTotal(selectResult.getTotal());
        page.setRecords(resultList);
        return page;
    }
}
